In [1]:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import cv2
from IPython.display import HTML, Video
from moviepy.editor import VideoFileClip
from PerspectiveTransformation import *
from LaneLines import *
In [2]:
images = []
images.append(mpimg.imread('./Project_data/test_images/straight_lines1.jpg'))
images.append(mpimg.imread('./Project_data/test_images/straight_lines2.jpg'))
images.append(mpimg.imread('./Project_data/test_images/test1.jpg'))
images.append(mpimg.imread('./Project_data/test_images/test2.jpg'))
images.append(mpimg.imread('./Project_data/test_images/test3.jpg'))
images.append(mpimg.imread('./Project_data/test_images/test4.jpg'))
images.append(mpimg.imread('./Project_data/test_images/test5.jpg'))
images.append(mpimg.imread('./Project_data/test_images/test6.jpg'))
In [3]:
def plot(img):
    hls = cv2.cvtColor(img, cv2.COLOR_RGB2HLS)
    f = plt.figure(figsize=(24, 9))
    ax1 = f.add_subplot(1, 3, 1)
    ax1.imshow(hls[:,:,0], cmap='gray', vmin=0, vmax=255)
    ax2 = f.add_subplot(1, 3, 2)
    ax2.imshow(hls[:,:,1], cmap='gray', vmin=0, vmax=255)
    ax3 = f.add_subplot(1, 3, 3)
    ax3.imshow(hls[:,:,2], cmap='gray', vmin=0, vmax=255)
    plt.figure()
    plt.imshow(img)
In [4]:
img = images[3]
birdeye = PerspectiveTransformation()
plt.imshow(img)
Out[4]:
<matplotlib.image.AxesImage at 0x15364685e80>
In [5]:
plot(img)
In [6]:
def threshold_rel(img, lo, hi):
    vmin = np.min(img)
    vmax = np.max(img)
    
    vlo = vmin + (vmax - vmin) * lo
    vhi = vmin + (vmax - vmin) * hi
    return np.uint8((img >= vlo) & (img <= vhi)) * 255

def threshold_abs(img, lo, hi):
    return np.uint8((img >= lo) & (img <= hi)) * 255
In [7]:
def test(img):
    tmp = birdeye.forward(img)
    tmp = np.copy(img)
    hls = cv2.cvtColor(tmp, cv2.COLOR_RGB2HLS)
    hsv = cv2.cvtColor(tmp, cv2.COLOR_RGB2HSV)
    h_channel = hls[:,:,0]
    l_channel = hls[:,:,1]
    s_channel = hls[:,:,2]
    v_channel = hsv[:,:,2]
    
    f = plt.figure(figsize=(24, 16))
    ax1 = f.add_subplot(2, 2, 1)
    ax1.imshow(tmp)
    ax2 = f.add_subplot(2, 2, 2)
    ax2.imshow(threshold_rel(l_channel, 0.8, 1.0), vmin=0, vmax=255, cmap='gray')
    
    ax3 = f.add_subplot(2, 2, 3)
    ax3.imshow(threshold_rel(s_channel, 0.5, 0.8), vmin=0, vmax=255, cmap='gray')
    
    ax4 = f.add_subplot(2, 2, 4)
    res = threshold_abs(h_channel, 20, 30)
    res &= threshold_rel(v_channel, 0.7, 1.0)
    ax4.imshow(res, vmin=0, vmax=255, cmap='gray')
In [8]:
test(images[1])
In [9]:
test(images[2])
In [10]:
test(images[3])
In [11]:
test(images[4])
In [12]:
img = images[1]
plt.imshow(img)
Out[12]:
<matplotlib.image.AxesImage at 0x1536680fdf0>
In [13]:
img1 = birdeye.forward(img)
plt.imshow(img1)
Out[13]:
<matplotlib.image.AxesImage at 0x15366da45e0>
In [14]:
hls = cv2.cvtColor(img1, cv2.COLOR_RGB2HLS)
hsv = cv2.cvtColor(img1, cv2.COLOR_RGB2HSV)
h_channel = hls[:,:,0]
l_channel = hls[:,:,1]
s_channel = hls[:,:,2]
v_channel = hsv[:,:,2]

f = plt.figure(figsize=(24, 16))
ax1 = f.add_subplot(2, 2, 1)
ax1.imshow(img1)
ax2 = f.add_subplot(2, 2, 2)
right_lane = threshold_rel(l_channel, 0.8, 1.0)

ax2.imshow(right_lane, vmin=0, vmax=255, cmap='gray')

ax3 = f.add_subplot(2, 2, 3)
left_lane = threshold_abs(h_channel, 20, 30)
left_lane &= threshold_rel(v_channel, 0.7, 1.0)
left_lane[:,550:] = 0
ax3.imshow(left_lane, vmin=0, vmax=255, cmap='gray')

img2 = left_lane | right_lane
ax4 = f.add_subplot(2, 2, 4)
ax4.imshow(img2, vmin=0, vmax=255, cmap='gray')
Out[14]:
<matplotlib.image.AxesImage at 0x15366ea3d00>
In [15]:
lanelines = LaneLines()
img3 = lanelines.forward(img2)
plt.imshow(img3)
Out[15]:
<matplotlib.image.AxesImage at 0x15366fc4c70>
In [16]:
img4 = birdeye.backward(img3)
out_img = cv2.addWeighted(img, 1, img4, 0.3, 0)
plt.imshow(out_img)
Out[16]:
<matplotlib.image.AxesImage at 0x15366dc1fd0>
In [17]:
birdeye = PerspectiveTransformation()
lanelines = LaneLines()
def process_image(img):
    # step 1
    img1 = birdeye.forward(img)
#     img1 = np.copy(img)
    
    # step 2
    hls = cv2.cvtColor(img1, cv2.COLOR_RGB2HLS)
    hsv = cv2.cvtColor(img1, cv2.COLOR_RGB2HSV)
    h_channel = hls[:,:,0]
    l_channel = hls[:,:,1]
    s_channel = hls[:,:,2]
    v_channel = hsv[:,:,2]

    right_lane = threshold_rel(l_channel, 0.8, 1.0)
    right_lane[:,:750] = 0

    left_lane = threshold_abs(h_channel, 20, 30)
    left_lane &= threshold_rel(v_channel, 0.7, 1.0)
    left_lane[:,550:] = 0

    img2 = left_lane | right_lane
    
    # step 3
    img3 = lanelines.forward(img2)
    
    # step 4
    img4 = birdeye.backward(img3)
    out_img = cv2.addWeighted(img, 1, img4, 1, 0)
    
    return out_img
In [18]:
tmp = process_image(images[5])
plt.imshow(tmp)
Out[18]:
<matplotlib.image.AxesImage at 0x1536679e5b0>
In [19]:
tmp = process_image(images[4])
plt.imshow(tmp)
Out[19]:
<matplotlib.image.AxesImage at 0x153668ba970>
In [20]:
tmp = process_image(images[0])
plt.imshow(tmp)
Out[20]:
<matplotlib.image.AxesImage at 0x15366689b20>
In [ ]: